/*******按下enter键创建li******/

var acOL = document.getElementById("acOL");
var coUL = document.getElementById("coUL");
var doPut = document.getElementById("DoList");

Array.prototype.remove = function(val){
	
	var index = this.indexOf(val);
	
	if(index > -1){
		
		this.splice(index,1);
	}
}


var arry = [];
var arry1 = [];

$("#DoList").bind("keydown",function(event){
	
	if(event.keyCode == 13 || event.keyCode == 30){
		
		if(doPut.value == ""){
			
			alert("内容不能为空");
		
		}else{
			
			var str = doPut.value;
			
			arry.push(str);
		
			setCookie("listL",{"ar":arry,"ar1":arry1},7);
			
			history.go(0);
		}
	}
});

check();

function check(){
	
	if(getCookie("listL")){
		
		arry = Array.from(getCookie("listL").ar);
		
		arry.forEach(function(str,ind){
			
			var li = document.createElement("li");
			acOL.appendChild(li);
			
			var aD = document.createElement("div");
			li.appendChild(aD);
			
			var inpT = document.createElement("input");
			li.appendChild(inpT);
			inpT.type = "checkbox"
			
			inpT.onclick = function(){
				
				arry1.push(str);
				acOL.removeChild(li);
				arry.remove(str);
				setCookie("listL",{"ar":arry,"ar1":arry1},7);
				history.go(0);
				
			}
			
			var span = document.createElement("span");
			li.appendChild(span);
			span.innerHTML = str;
			
			var a = document.createElement("a");
			li.appendChild(a);
			a.innerHTML = "-";
			a.num = ind;
			
			a.onclick = function(){
				
				acOL.removeChild(li);
				arry.remove(str);
				setCookie("listL",{"ar":arry,"ar1":arry1},7);
				history.go(0);
			}
		});
		
		arry1 = Array.from(getCookie("listL").ar1);
		
		for(var i = 0; i< arry1.length; i++){
			
			var li1 = document.createElement("li");
			coUL.appendChild(li1);
			
			var aD1 = document.createElement("div");
			li1.appendChild(aD1);
			
			var inpT1 = document.createElement("input");
			li1.appendChild(inpT1);
			inpT1.type = "checkbox"
			inpT1.checked = true;
			inpT1.num = i;
			
			inpT1.onchange = function(){
				
				arry.push(arry1[this.num]);
				coUL.removeChild(li1);
				arry1.remove(arry1[this.num]);
				setCookie("listL",{"ar":arry,"ar1":arry1},7);
				history.go(0);
			}
			
			var span1 = document.createElement("span");
			li1.appendChild(span1);
			span1.innerHTML = arry1[i];
			
			var a1 = document.createElement("a");
			li1.appendChild(a1);
			a1.innerHTML = "-";
			a1.num = i;
			
			a1.onclick = function(){
				
				coUL.removeChild(li1);
				arry1.remove(arry1[a1.num]);
				setCookie("listL",{"ar":arry,"ar1":arry1},7);
				history.go(0);
			}
			
		}
		
		$("#aNum").html(arry.length);
		$("#cNum").html(arry1.length);
		
	}else{
		
		arry = [];
		arry1 = [];
	}
}








